﻿' Copyright © Microsoft Corporation.  All Rights Reserved.
' This code released under the terms of the 
' Microsoft Public License (MS-PL, http://opensource.org/licenses/ms-pl.html.)
' <copyright file="AccessibleListBox.vb" company="Microsoft">
'     Copyright © Microsoft Corporation. All rights reserved.
' </copyright>
' <author>Microsoft Expression Encoder Team</author>

Imports Microsoft.VisualBasic
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Automation
Imports System
Imports System.Windows.Media

Namespace ExpressionMediaPlayer
	''' <summary>
	''' allows screen reader to read items defined in a listbox which are not listbox items.
	''' </summary>
	Public Class AccessibleListBox
		Inherits ListBox
		Private Shared Function GetVisualParentByType(ByVal dpo As DependencyObject, ByVal type As Type) As DependencyObject
			Do While dpo IsNot Nothing AndAlso dpo.GetType() IsNot type
				dpo = VisualTreeHelper.GetParent(dpo)
			Loop

			Return dpo
		End Function

		Protected Overrides Sub PrepareContainerForItemOverride(ByVal element As DependencyObject, ByVal item As Object)
			MyBase.PrepareContainerForItemOverride(element, item)
			Dim accessible As IAccessible = TryCast(item, IAccessible)
			If accessible IsNot Nothing Then
				Dim btnFake As New Button()
				btnFake.IsEnabled = False
				btnFake.Visibility = Visibility.Collapsed
				btnFake.SetValue(AutomationProperties.NameProperty, accessible.AccessibilityText)
				element.SetValue(AutomationProperties.LabeledByProperty, CType(btnFake, UIElement))
			End If

			Dim lbi As ListBoxItem = CType(GetVisualParentByType(element, GetType(ListBoxItem)), ListBoxItem)

		End Sub
	End Class
End Namespace
